﻿using Abp.Application.Services;
using Abp.Dependency;
using Abp.Runtime.Validation.Interception;
using Castle.DynamicProxy;
using Hangfire.Annotations;
using System;
using System.Collections.Generic;
using System.Text;

namespace ARchGL.Platform.Web.Validation
{
    public class JN_ValidationInterceptor : IInterceptor
    {
        private readonly IIocResolver _iocResolver;

        public JN_ValidationInterceptor(IIocResolver iocResolver)
        {
            _iocResolver = iocResolver;
        }

        public void Intercept(IInvocation invocation)
        {
            //if (AbpCrossCuttingConcerns.IsApplied(invocation.InvocationTarget, AbpCrossCuttingConcerns.Validation))
            //{
            //    invocation.Proceed();
            //    return;
            //}
            using (var validator = _iocResolver.ResolveAsDisposable<MethodInvocationValidator>())
            {
                validator.Object.Initialize(invocation.MethodInvocationTarget, invocation.Arguments);
                validator.Object.Validate();
            }

            invocation.Proceed();
        }

        public static bool IsApplied([NotNull] object obj, [NotNull] string concern)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (concern == null)
            {
                throw new ArgumentNullException(nameof(concern));
            }

            return (obj as IAvoidDuplicateCrossCuttingConcerns)?.AppliedCrossCuttingConcerns.Contains(concern) ?? false;
        }
    }
}
